import { Public } from '@/decorators/visit.decorator';
import { Body, Controller, Get, Post, Query, Req } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { Request } from 'express';
import { WatchUserDto } from './dto/watch-user.dto';
import { UserService } from './user.service';

@ApiTags('用户管理')
@Controller('user')
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly jwtService: JwtService,
  ) {}

  @ApiOperation({ summary: '获取用户信息', description: '获取用户信息' })
  @Get('/getUserInfo')
  @ApiBearerAuth()
  getUserInfo(@Req() req) {
    return this.userService.getPersonalMessage(Number(req.user.id), 0);
  }

  @ApiOperation({
    summary: '通过用户id获取用户信息',
    description: '通过用户id获取用户信息',
  })
  @ApiBearerAuth()
  @Get('/gerUserinfoByUserId')
  gerUserinfoByUserId(@Query('userId') userId: number) {
    return this.userService.gerUserinfoByUserId(Number(userId));
  }

  @ApiOperation({
    summary: '关注用户',
  })
  @Post('/watchUser')
  @ApiBearerAuth()
  watchUser(@Req() req, @Body() watchUserDto: WatchUserDto) {
    Object.assign(watchUserDto, {
      userId: req.user.id,
    } as WatchUserDto);
    return this.userService.watchUser(watchUserDto);
  }

  @Public()
  @ApiOperation({
    summary: '获取用户个人主页信息',
  })
  @ApiQuery({
    name: 'id',
    description: '用户id',
    type: Number,
    example: 0,
  })
  @Get('/getPersonalMessage')
  getPersonalMessage(@Req() req: Request, @Query('id') id: number) {
    let userId = 0;
    try {
      const result = this.jwtService.decode(
        String(req.headers.authorization).replace('Bearer ', ''),
      );
      if (result) {
        console.log(result);
        if ((result as Record<string, any>)?.id) {
          userId = Number((result as Record<string, any>).id);
        }
      }
    } catch (error) {
      console.log('error', error);
    }
    return this.userService.getPersonalMessage(id, userId);
  }

  @Public()
  @ApiOperation({
    summary: '关注的人',
  })
  @ApiQuery({
    name: 'userId',
    type: Number,
    description: '用户id',
    example: 0,
  })
  @Get('/getWathUser')
  getWathUser(@Req() req, @Query('userId') userId = 0) {
    let _userId = Number(userId);
    try {
      const result = this.jwtService.decode(
        String(req.headers.authorization).replace('Bearer ', ''),
      );
      if (result) {
        console.log(result);
        if ((result as Record<string, any>)?.id) {
          _userId = Number((result as Record<string, any>).id);
        }
      }
    } catch (error) {
      console.log('error', error);
    }
    return this.userService.getWathUser(_userId);
  }

  @Public()
  @ApiOperation({
    summary: '我的粉丝',
  })
  @ApiQuery({
    name: 'userId',
    type: Number,
    description: '用户id',
    example: 0,
  })
  @Get('/getFans')
  getFans(@Req() req, @Query('userId') userId = 0) {
    let _userId = Number(userId);
    try {
      const result = this.jwtService.decode(
        String(req.headers.authorization).replace('Bearer ', ''),
      );
      if (result) {
        console.log(result);
        if ((result as Record<string, any>)?.id) {
          _userId = Number((result as Record<string, any>).id);
        }
      }
    } catch (error) {
      console.log('error', error);
    }
    return this.userService.getFans(_userId);
  }
}
